<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Tools\Func;
use Illuminate\Support\Facades\DB;

use App\Models\News;
use App\Models\NewsCategory;

class NewsCategoryController extends Controller
{
    use Func; 

    // 列表
    public function list(Request $request)
    {
        $data = NewsCategory::orderBy('id', 'desc')->paginate($request->limit ?? 10);
        return $this->returnJson(0, 'success', $data);
    }
	
	public function getList(Request $request)
    {
        $data = NewsCategory::orderBy('id', 'desc')->get();
        return $this->returnJson(0, 'success', $data);
    }

    // 添加
    public function add(Request $request)
    {
        if (!$res = NewsCategory::create([
            'category_name' => $request->category_name,
            'created_at' => date('Y-m-d H:i:s')
        ])) {
            return $this->returnJson(1, '添加失败', $res);
        }

        return $this->returnJson(0, '添加成功', $res);
    }
	
	
	// 删除
	public function del(Request $request)
	{
	    if (!NewsCategory::where('id', $request->id)->delete()) {
	        return $this->returnJson(1, '删除失败');
	    }
	
	    return $this->returnJson(0, '删除成功');
	}
	
	// 获得
	public function get(Request $request)
	{
		// dump($request->all());exit;
	    $res = NewsCategory::where('id', $request->id)->first();
	
	    return $this->returnJson(0, 'success', $res);
	}
	
	// 编辑
	public function edit(Request $request)
	{
	    // dd($request->addAccount);
	    if (!$res = NewsCategory::where('id', $request->id)->update([
	        'category_name' => $request->category_name,
			
	    ])) {
	        return $this->returnJson(1, '修改失败', $res);
	    }
	
	    return $this->returnJson(0, '修改成功', $res);       
	}

}
